﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Windows.Forms;
using WeForms.Controls;
using WeForms.Design;
using WeForms.View;

namespace WeForms
{
    [DefaultEvent("ViewChanged")]
    public class WeTabContainer : ContainerControl
    {
        private static readonly StringFormat TitleFormat = new StringFormat
                                                               {
                                                                   LineAlignment = StringAlignment.Center,
                                                                   Trimming = StringTrimming.EllipsisCharacter
                                                               };
        private readonly SideBar _sideBar;
        private readonly TouchButton _closeButton;
        private readonly WeTabSideBarControlList _controlList;

        private readonly WeTabViewList _viewList = new WeTabViewList();
        
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(WeTabSideBarControlListEditor), typeof(UITypeEditor))]
        public WeTabSideBarControlList SideBarControls
        {
            get { return _controlList; }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Browsable(false)]
        public WeTabViewList Views
        {
            get { return _viewList; }
        }

        public WeTabView CurrentView
        {
            get
            {
                if (!_viewList.ContainsKey(CurrentViewId)) return null;
                return _viewList[CurrentViewId];
            }
        }

        [DefaultValue(-1)]
        public int ViewSpecificControlsIndex { get; set; }

        public string CurrentViewId { get; private set; }

        public WeTabContainer()
        {
            SetStyle(ControlStyles.ContainerControl, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.FixedWidth, true);
            SetStyle(ControlStyles.FixedHeight, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            base.DoubleBuffered = true;

            CurrentViewId = "";

            _sideBar = new SideBar();
            _sideBar.Dock = DockStyle.Left;
            _controlList = new WeTabSideBarControlList(_sideBar.Controls);
            Controls.Add(_sideBar);

            _closeButton = new TouchButton();
            _closeButton.BackColor = WeTabTheme.SideBarBackground;
            _closeButton.AutoSize = false;
            _closeButton.ColorScheme = TouchButtonColor.WeTab;
            _closeButton.Size = new Size(23,23);
            _closeButton.Location = new Point(1210, 10);
            _closeButton.Click += delegate
                                      {
                                          if(ParentForm != null)
                                              ParentForm.Close();
                                      };
            Controls.Add(_closeButton);
            

            ViewSpecificControlsIndex = -1;

            Margin = new Padding(0);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_sideBar != null) _sideBar.Dispose();
                if (_viewList != null)
                {
                    string[] keys = _viewList.Keys.ToArray();
                    foreach (string key in keys)
                    {
                        WeTabView view = _viewList[key];
                        _viewList.Remove(key);
                        view.Dispose();
                    }
                }
            }
            base.Dispose(disposing);
        }

        public override Size GetPreferredSize(Size proposedSize)
        {
            return base.GetPreferredSize(WeTabTheme.FullScreenAppSize);
        }

        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            base.SetBoundsCore(x, y, WeTabTheme.FullScreenAppSize.Width, WeTabTheme.FullScreenAppSize.Height, specified);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            e.Graphics.Clear(WeTabTheme.SideBarBackground);

            if (ParentForm == null) return;


            int x = WeTabTheme.SideBarWidth + 15;
            Rectangle rect = new Rectangle(x, 5, Width - x - 50, 35);
            using (SolidBrush b = new SolidBrush(WeTabTheme.FormCaptionColor))
            {
                e.Graphics.DrawString(ParentForm.Text, WeTabTheme.FormFont, b, rect, TitleFormat);
            }

        }

        public void DisplayView(string id)
        {
            Console.WriteLine("Switching to View {0}");

            if (!_viewList.ContainsKey(id) || id == CurrentViewId) return;
            WeTabView oldView = CurrentView;
            CurrentViewId = id;
            foreach (WeTabSideBarControl control in _controlList)
            {
                if (control is WeTabSideBarViewButton)
                {
                    WeTabSideBarViewButton button = (WeTabSideBarViewButton)control;
                    button.Checked = button.View == id;
                }
            }

            // remove old view
            SuspendLayout();
            _sideBar.SuspendLayout();
            if (oldView != null)
            {
                oldView.OnViewDeactivating();
                Controls.Remove(oldView);
                foreach (WeTabSideBarControl control in oldView.ViewSpecificControls)
                {
                    SideBarControls.Remove(control);
                }
                oldView.OnViewDeactivated();

            }

            if (CurrentView == null) return;

            CurrentView.OnViewActivating();
            CurrentView.Dock = DockStyle.None;
            CurrentView.Location = new Point(WeTabTheme.SideBarWidth, 40);
            CurrentView.Size = new Size(Width - CurrentView.Location.X, Height - CurrentView.Location.Y);
            Controls.Add(CurrentView);
            Controls.SetChildIndex(CurrentView, 0);

            foreach (WeTabSideBarControl control in CurrentView.ViewSpecificControls)
            {
                if (ViewSpecificControlsIndex >= 0)
                    SideBarControls.Insert(ViewSpecificControlsIndex, control);
                else
                    SideBarControls.Add(control);
            }

            CurrentView.OnViewActivated();
            ResumeLayout(true);
            _sideBar.ResumeLayout(true);

            OnViewChanged(EventArgs.Empty);
        }

        public event EventHandler ViewChanged;
        protected virtual void OnViewChanged(EventArgs e)
        {
            if (ViewChanged != null)
                ViewChanged(this, e);
        }

        public void RegisterView(string key, WeTabView view)
        {
            view.ParentContainer = this;
            Views.Add(key, view);
        }
    }
}
